package com.syh.jn.scm.infrastructure.repository.impl.supplier.base;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.base.Salesman;
import com.syh.jn.scm.domain.supplier.repository.base.SalesmanRepository;
import com.syh.jn.scm.dto.supplier.base.salesMan.SalesmanVo;
import com.syh.jn.scm.infrastructure.repository.model.base.SalesmanModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class SalesmanRepositoryImpl extends AbstractAggregateRepository<Salesman, SalesmanModel, Long> implements SalesmanRepository {
  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(SalesmanModel.class, "salesMan", null),
              "salesTeam.name as salesTeamName"
      );
      FROM("T_BASE_SALESMAN salesMan");
      LEFT_OUTER_JOIN("T_BASE_SALES_TEAM salesTeam on salesTeam.ID = salesMan.SALES_TEAM_ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return SalesmanVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String,String> params = new HashMap<>();
    params.put("name","salesMan.NAME");
    params.put("mobile","salesMan.MOBILE");
    params.put("status","salesMan.STATUS");
    params.put("id","salesMan.ID");
    params.put("businessEntityId","salesMan.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return params.get(propertyName);
    });
    return parse;
  }
}
